from strategy.StrategyTemplate import StrategyTemplate
from strategy import strategy_filter
from utils.pd_show_utils import df_table
from utils.db import DbHandler
from data_tool.table_data_tools import up_price
import pandas as pd


class BondActive(StrategyTemplate):
    """
    活跃债策略:
        计算3天和5天平均换手，综合筛选转债
    """

    def __init__(self):
        super(BondActive, self).__init__()
        self.strategy_name = "活跃债策略"
        self.strategy_descrption = "活跃债策略"

    def run(self):
        db = DbHandler()
        db.create_session()

        pd_bond_alive = pd.read_sql_table('JSL_bonds_alive', con=db.engine)
        pd_bond_alive = pd_bond_alive[['bond_id', 'bond_nm', 'price', 'premium_rt',
                                       'bond_stdevry', 'volatility_rate', 'curr_iss_amt',
                                       'year_left', 'maturity_dt',  'ytm_rt']]
        pd_bond_alive = up_price(db, pd_bond_alive)

        # 清掉原来的索引
        pd_bond_alive.reset_index(inplace=True, drop=True)
        db.close_session()
        df_table(pd_bond_alive, index='index')

    def api(self):
        db = DbHandler()
        db.create_session()

        pd_bond_alive = pd.read_sql_table('JSL_bonds_alive', con=db.engine)
        pd_bond_alive = strategy_filter.filter_base(pd_bond_alive)
        pd_bond_alive = pd_bond_alive[['bond_id', 'bond_nm', 'price','increase_rt','premium_rt',
                                       'bond_stdevry', 'volatility_rate', 'curr_iss_amt',
                                       'year_left', 'maturity_dt',  'ytm_rt']]

        pd_bond_alive = up_price(db, pd_bond_alive)

        pd_bond_alive = pd_bond_alive.fillna(0)
        # 清掉原来的索引
        pd_bond_alive.reset_index(inplace=True, drop=True)
        db.close_session()
        return pd_bond_alive


if __name__ == '__main__':
    ins = BondActive()
    # ins.topN = 20
    ins.run()
